﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using ScarecrowLibrary;

namespace Scarecrow_Network_Manager
{
    /// <summary>
    /// Logica di interazione per SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        enum Languages
        {
            Italiano = 0x0010,
            English = 0x0009,
            Español = 0x0C0A
        }

        bool updating = false;

        public SettingsWindow()
        {
            InitializeComponent();
        }

        private void chkAutostart_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                scApplicationAutostarter.SetAutoStart(Global.ProductName, Global.PublisherName, new string[] { "-s" });
                Properties.Settings.Default.AutoStart = (bool)this.chkAutostart.IsChecked;

                Properties.Settings.Default.Save();
            }
            catch (Exception exc)
            {
                Global.NotifyCrash(exc);
            }
        }

        private void chkNotify_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.NotifyHide = (bool)this.chkNotify.IsChecked;
            Properties.Settings.Default.Save();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                this.chkAutostart.IsChecked = Properties.Settings.Default.AutoStart;
                this.chkNotify.IsChecked = Properties.Settings.Default.NotifyHide;

                cmbLanguage.ItemsSource = Enum.GetNames(typeof(Languages));
                updating = true;
                cmbLanguage.SelectedItem = ((Languages)Properties.Settings.Default.Language).ToString();
                updating = false;
            }
            catch (Exception exc)
            {
                Global.NotifyCrash(exc);
            }
        }

        private void chkAutostart_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                scApplicationAutostarter.UnSetAutoStart(Global.ProductName);
                Properties.Settings.Default.AutoStart = (bool)this.chkAutostart.IsChecked;

                Properties.Settings.Default.Save();
            }
            catch (Exception exc)
            {
                Global.NotifyCrash(exc);
            }
        }

        private void chkNotify_Unchecked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.NotifyHide = (bool)this.chkNotify.IsChecked;
            Properties.Settings.Default.Save();
        }

        private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(e.Uri.AbsoluteUri));
        }

        private void cmbLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbLanguage.SelectedItem != null && !updating)
            {
                Properties.Settings.Default.Language = (int)Enum.Parse(typeof(Languages),cmbLanguage.SelectedItem.ToString());
                Properties.Settings.Default.Save();

                if (MessageBox.Show(Properties.Resources.RebootNeeded, Properties.Resources.Warning, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
                    Application.Current.Shutdown();
                }
            }
        }
    }
}
